#include "configdialog.h"

ConfigDialog::ConfigDialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
    ui->setupUi(this);

	qRegisterMetaType<UpdateWhat>("UpdateWhat");

	QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(SendChatMessage()));
	QObject::connect(ui->pushButtonConnect,SIGNAL(clicked()),this,SLOT(AttemptConnection()));
	QObject::connect(ui->pushButtonCreateServer,SIGNAL(clicked()),this,SLOT(AttemptServerStart()));
	QObject::connect(this,SIGNAL(SendUpdateGUI(const char* , UpdateWhat)),this,SLOT(UpdateGUI(const char *, UpdateWhat)),Qt::QueuedConnection);

	ThisUI = ui;

	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),0,1); // fwd
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),1,1); // bkfwd
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),2,1); // left
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),3,1); // right
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),4,1); // up
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),5,1); // down
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),6,1); // shoot
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),7,1); // menu

	strYes = "Ja";
	strNo = "Nein";

}

ConfigDialog::~ConfigDialog()
{
    delete ui;
}

void ConfigDialog::SendChatMessage()
{
/*	// get the chat string
	QString str = ui->lineEdit_ChatLine->text();

        const char* Message;

        // convert
	QByteArray MessageByteArray = str.toAscii();
	Message = MessageByteArray.data();

	// send to client (which sends the message via rpc)
//	CManageNetworkSingle::getInstance()->m_pClient->SendChatMessage(Message);
*/}


void ConfigDialog::receive_UpdateServerLog(QString logMessage)
{
    UpdateServerLog(logMessage);
}

void ConfigDialog::receive_UpdateChatWin(QString chatMessage)
{
    UpdateChatWin(chatMessage);
}

void ConfigDialog::UpdateServerLog(QString logMessage)
{
    QString DisplayText;
    DisplayText = ui->plainTextServerLog->toPlainText();
    DisplayText += "\r\n";
    DisplayText += logMessage;
    ui->plainTextServerLog->setPlainText(DisplayText);
}

void ConfigDialog::UpdateChatWin(QString chatMessage)
{
    QString DisplayText;
    DisplayText = ui->plainTextChatWin->toPlainText();
    DisplayText += "\r\n";
    DisplayText += chatMessage;
    ui->plainTextChatWin->setPlainText(DisplayText);
}

void ConfigDialog::AttemptConnection()
{
        QLineEdit* qualleServer = ThisUI->lineEditServer;
	       
        // Create a Thread for the whole thing to run in, then use it to
        // start the server.
        m_ClientThread = new QThread;
        m_NetworkClient = new CClient;

        m_NetworkClient->SetCallbackGUI(this);

        m_NetworkClient->ServerAdress = qualleServer->text();

        m_NetworkClient->moveToThread(m_ClientThread);
        connect(m_ClientThread, SIGNAL(started()), m_NetworkClient, SLOT(start()));
        m_ClientThread->start();

}

void ConfigDialog::AttemptServerStart()
{
	int Port;

	// convert port number
	QLineEdit* quallePort = ui->lineEditServerPort;	
	QString qsPort = quallePort->text();
	
	bool ok = false;
	Port = qsPort.toUShort(&ok,10);
	// Conversion ok?
	if(ok == false)
	{
            // C++ Exceptions again. What to do?
	}

        // Create a Thread for the whole thing to run in, then use it to
        // start the server.
        m_ServerThread = new QThread;
        m_NetworkServer = new CServer;

        m_NetworkServer->SetCallbackGUI(this);

        if(ok)
            m_NetworkServer->Port = Port;
        // if the conversion was ok, use the port,
        // if not leave it. The default port 8000 will be used

        m_NetworkServer->moveToThread(m_ServerThread);
        connect(m_ServerThread, SIGNAL(started()), m_NetworkServer, SLOT(start()));
        m_ServerThread->start();
}

void ConfigDialog::saveConfig()
{
	QDir qdirDir;
	QFile qfileVideoConfigFile("config/video.cfg");
	QFile qfileInputConfigFile("config/input.cfg");
	QTextStream qtxtstrm;
	QString strTemp;

	qdirDir.mkpath("config"); // create the path

	qfileVideoConfigFile.open(QIODevice::WriteOnly);
	qtxtstrm.setDevice(&qfileVideoConfigFile);

	strTemp = ui->cbResolution->currentText();
	qtxtstrm << "strResolutionX" << " " << strTemp.mid(0, strTemp.indexOf("x") ) << "\n";
	qtxtstrm << "strResolutionY" << " " << strTemp.mid(strTemp.indexOf("x") + 1 ) << "\n";
	qtxtstrm << "bFullscreen" << " " << (ui->cbFullScreen->currentText() == strYes ? "true" : "false") << "\n";
	qtxtstrm << "strRenderSystem" << " " << ui->cbRender->currentText() << "\n";
	qtxtstrm << "bVSync" << " " << (ui->cbVSync->currentText() == strYes ? "true" : "false") << "\n";
	qtxtstrm << "iBPP" << " " << ui->cbBPP->currentText() << "\n";

	qfileVideoConfigFile.close();

	qfileInputConfigFile.open(QIODevice::WriteOnly);
	qtxtstrm.setDevice(&qfileInputConfigFile);
	qtxtstrm << "cForward" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(0,1)->widget())->text() << "\n";
	qtxtstrm << "cBackward" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(1,1)->widget())->text() << "\n";
	qtxtstrm << "cLeft" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(2,1)->widget())->text() << "\n";
	qtxtstrm << "cRight" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(3,1)->widget())->text() << "\n";
	qtxtstrm << "cUp" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(4,1)->widget())->text() << "\n";
	qtxtstrm << "cDown" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(5,1)->widget())->text() << "\n";
	qtxtstrm << "cRun" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(6,1)->widget())->text() << "\n";
	qtxtstrm << "cMenu" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(7,1)->widget())->text() << "\n";


	qfileInputConfigFile.close();
}

void ConfigDialog::setConfig(ConfigContainer *configContainer)
{
	QTextStream tsTemp;
	QString strTemp;

	tsTemp.setString(&strTemp);

	tsTemp << configContainer->videoConfig.iResolutionX << "x" << configContainer->videoConfig.iResolutionY;
	ui->cbResolution->setCurrentIndex( ui->cbResolution->findText(strTemp) );

	strTemp.clear();
	tsTemp << configContainer->videoConfig.iBPP;
	ui->cbBPP->setCurrentIndex( ui->cbBPP->findText(strTemp) );

	strTemp.clear();
	tsTemp << (configContainer->videoConfig.bFullscreen ? strYes : strNo); // Now thats one cool context for the "?"-operator!
	ui->cbFullScreen->setCurrentIndex( ui->cbFullScreen->findText(strTemp) );

	strTemp.clear();
	tsTemp << configContainer->videoConfig.carrRenderMode;
	ui->cbRender->setCurrentIndex( ui->cbRender->findText(strTemp) );

	strTemp.clear();
	tsTemp << (configContainer->videoConfig.bVSync ? strYes : strNo);
	ui->cbVSync->setCurrentIndex( ui->cbVSync->findText(strTemp) );

	// set input
	strTemp.clear();
        tsTemp << configContainer->keyMapping.forward.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(0,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.backward.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(1,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.left.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(2,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.right.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(3,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.up.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(4,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.down.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(5,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.run.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(6,1)->widget())->setText(strTemp);

	strTemp.clear();
        tsTemp << configContainer->keyMapping.menu.c_str();
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(7,1)->widget())->setText(strTemp);

}

